﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Models;

namespace KorbeRTR
{
    public partial class FormTemperatureRecord : Form
    {
        public FormTemperatureRecord()
        {
            InitializeComponent();
        }

        private void FormTemperatureRecord_Load(object sender, EventArgs e)
        {
            InitGrid(grid1);

            DTP_End.Value = DateTime.Now.Date.Date;
            DTP_Start.Value = DTP_End.Value.Date.AddDays(-2);

            var list = TemperatureRecord.Select(DTP_Start.Value.Date, DTP_End.Value.Date.AddDays(1));
            list.Sort((a, b) => a.Time.CompareTo(b.Time));
            UpdateGrid(grid1, list);
        }

        private void InitGrid(FlexCell.Grid grid)
        {
            string line = ConfigurationManager.AppSettings["LINE"];

            grid.AutoRedraw = false;
            grid.ExtendLastCol = false;
            grid.DisplayRowNumber = true;

            // 设置行数/列数
            grid.Rows = 1;
            grid.Cols = 13;

            //这个包围区间要减1 居中
            grid.Range(0, 0, grid1.Rows - 1, grid1.Cols - 1).Alignment = FlexCell.AlignmentEnum.CenterCenter;
            for (int i = 0; i < grid1.Cols; i++)
            {
                grid.Cell(0, i).WrapText = true;
            }
            // 设置行高/列宽


            grid.Column(0).Width = 40;
            grid.Row(0).Height = 30;
            grid.Column(1).Width = 160;

            // 设置单元格文字
            int col = 0;
            grid.Cell(0, col++).Text = "序号";
            grid.Cell(0, col++).Text = "时间";
            grid.Cell(0, col++).Text = "除油";
            grid.Cell(0, col++).Text = "热水洗";
            grid.Cell(0, col++).Text = "整流机冷却";
            grid.Cell(0, col++).Text = "酸洗槽";
            grid.Cell(0, col++).Text = "镀铜";
            grid.Cell(0, col++).Text = "微蚀";
      
            grid.Cell(0, col++).Text = "烘干加热";
            grid.Cell(0, col++).Text = "酸浸";
            grid.Cell(0, col++).Text = "抗氧化";
            grid.Cell(0, col++).Text = "热水槽";

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        private void UpdateGrid(FlexCell.Grid grid, List<TemperatureRecord> list)
        {
            list.Sort((a, b) => b.Time.CompareTo(a.Time));
            bool isB = ConfigurationManager.AppSettings["LINE"] == "B";

            grid.AutoRedraw = false;
            grid.Rows = list.Count + 1;

            for(int i = 0;i<list.Count;i++)
            {
                int col = 1;
                grid.Cell(i+1, col++).Text = list[i].Time.ToString();
                grid.Cell(i+1, col++).Text = list[i].除油温度.ToString("0.0");
                grid.Cell(i+1, col++).Text = list[i].热水洗温度.ToString("0.0");
                grid.Cell(i+1, col++).Text = list[i].整流机冷却.ToString("0.0");
                grid.Cell(i+1, col++).Text = list[i].酸洗槽.ToString("0.0");
                grid.Cell(i+1, col++).Text = list[i].镀铜.ToString("0.0");
                grid.Cell(i+1, col++).Text = list[i].微蚀.ToString("0.0");
        
                grid.Cell(i + 1, col++).Text = list[i].烘干加热.ToString("0.0");
                grid.Cell(i + 1, col++).Text = list[i].酸浸.ToString("0.0");
                grid.Cell(i + 1, col++).Text = list[i].抗氧化.ToString("0.0");
                grid.Cell(i + 1, col++).Text = list[i].热水槽.ToString("0.0");
            }

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        //检测窗体是否打开
        private bool CheckForm(string formName)
        {
            bool bl = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formName)
                {
                    bl = true;
                    f.Activate();
                    break;
                }
            }
            return bl;
        }

        /// <summary>
        /// 显示曲线
        /// </summary>
        private void tsCurve_Click(object sender, EventArgs e)
        {
            try
            {
                bool bl = CheckForm(typeof(FormTemperatureCurve).Name);
                if (!bl)
                {
                    var form = new FormTemperatureCurve();
                    form.ShowDialog();
                }
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message+ Environment.NewLine + err.StackTrace);
            }
        }

        private void DTP_Start_ValueChanged(object sender, EventArgs e)
        {
            if (Math.Abs((DTP_Start.Value.Date - DTP_End.Value.Date.AddDays(1)).Days) > 7)
            {
                DTP_End.Value = DTP_Start.Value.Date.Date;
            }

            var list = TemperatureRecord.Select(DTP_Start.Value.Date, DTP_End.Value.Date.AddDays(1));
            UpdateGrid(grid1, list);
        }

        private void DTP_End_ValueChanged(object sender, EventArgs e)
        {
            if (Math.Abs((DTP_Start.Value.Date - DTP_End.Value.Date.AddDays(1)).Days) > 7)
            {
                MessageBox.Show("查询时间不能超过7天");
                return;
            }

            var list = TemperatureRecord.Select(DTP_Start.Value.Date, DTP_End.Value.Date.AddDays(1));
            UpdateGrid(grid1, list);
        }

        private void tsExport_Click(object sender, EventArgs e)
        {
            if (grid1.ExportToExcel("", true, true))
            {
                MessageBox.Show("输出成功", "导出Excel", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
